<?php
/*
general url: content_type(0)/operation(1)/title(2)/keywords(3)
operations: search(s), read(r), browse(b)

samples:

read url  : r/book/harry_potter/pdf
search url: s/book/1/harry+potter
browse url: b/book/1
home url  : b/
import sitemap url: admin/isitemap
our sitemap index url: sitemap_index.xml
individual sitemaps: sitemap/sitemap_12.xml
*/
include('include/util.php');
include('include/services.php');
include('scripts/scripts.php');
include('scripts/download.php');
include('scripts/dl.php');
include('scripts/primative.php');


/*---------------------Start Information to be printed in forms----------------------------------*/
$data;
/*---------------------End   Information to be printed in forms----------------------------------*/

init();

function init()
{
    include('include/config.php');
    $_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    if ($folder_url != '/')
        $_SERVER['REQUEST_URI_PATH'] = str_replace($folder_url, '', $_SERVER['REQUEST_URI_PATH']);
    $segments = explode('/', trim($_SERVER['REQUEST_URI_PATH'], '/'));

    $operation = $segments[0];
    $content_type = $segments[1];
    $title = $segments[2];
    $page = $segments[2];
    $keywords = $segments[3];
    $admin_operation = $segments[1];
    $sitemap = $segments[1];

    //copyrighted stuff elimination
    $copyright = array('movies/ladies-and-gentleman-2013', 'mac/paw-http-client');

    foreach($copyright as $c)
        if(strpos($_SERVER['REQUEST_URI_PATH'], $c) !== FALSE)
        {
            echo "The requested content has been removed permanently due to reported copyright infringement.";
            exit();
        }

	$udnurl = "";


    if ($operation == 'b') {
        /*
            browse content_type
            from the table sitemap_data
            get number after p
            number of results in a page = x
            multiply this number by number of results in a page
            l = p * x;
            go to line number l in the sitemap, display links from there up to l + x;
            forget about images for now
            print them in a nice format
        */
        if ($content_type)
		{
			if($_POST['search'])
			{
				$search = str_replace(" ","-",$_POST['search']);
				header( "Location: ".$folder_url."b/$content_type/1/$search" );
                exit();
			}
			/*if($keywords)
                $_POST['search'] = $keywords;*/

            $results = services::browse_sitemap_data($content_type, $page_size, $page, $keywords);
		}
        else
            $results = services::browse_types();
        include('forms/read.php');
        //echo 'browsing '.$content_type.' in page: ' .$page;
    }
    else if ($operation == 'r') {
		$expire=60*60*24*30;// seconds, minutes, hours, days
		header('Pragma: public');
		header('Cache-Control: maxage='.$expire);
		header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expire) . ' GMT');
		header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time() - 60*60*24*1) . ' GMT');
        /*
            display information for this content_type
            use the url to get info from sitemap_data
            data is: poster, description, title

            use search to display similar content_type.
            -----------------------------------------
        */
        $udnurl = $operation . '/' . $content_type . '/' . $title;

        $data = services::get_sitemap_data($content_type, $udnurl);
        if ($data != null) {
            //echo 'reading '.$content_type.': ' .$title;
            include('forms/read.php');
        }
        else {
            //services::page_not_found();
            include('forms/read.php');
        }

    }
    else if ($operation == 'admin') {
        if ($admin_operation == 'isitemap') {
            /*
             * download sitemap_url locally
            * set the url to local url path
            * add the values to sitemap_meta
            */
            if ($_POST != null) {
                services::insert_sitemap_meta();
            }
            services::include_form('isitemap');
        }
        else if ($admin_operation == 'isitemap_status') {
            echo "last update: " . date('h:i:s') . " <table>";

            //quick and dirty way get last row thing...
            $con = util::open_connection();
            $sitemap_meta_id = null;
            $result = mysql_query("SELECT sitemap_meta_id FROM process_status ORDER BY sitemap_meta_id DESC LIMIT 0,1");
            while ($row = mysql_fetch_array($result))
                $sitemap_meta_id = $row['sitemap_meta_id'];
            util::close_connection($con);

            //echo $sitemap_meta_id; exit();
            $process = services::get_process_status($sitemap_meta_id);
            foreach ($process as $id => $value) {
                if (!is_numeric($id)) //do not echo numeric row index
                    echo "<tr><td>$id</td><td>$value</td></tr>";
            }
            echo "</table>";
        }
        else if ($admin_operation == 'txpath') {
            services::include_form('txpath');
        }
        else if($admin_operation == 'log')
        {
            include($log_file);
        }
        else if($admin_operation == 'clearlog')
        {
            file_put_contents($log_file, "");
        }
		else if ($admin_operation == 'stats')
		{
			services::include_form('stats');
		}
		else if ($admin_operation == '912')
		{
			services::include_form('test');
		}
        else if($admin_operation == 'inform')
        {
            //echo 'im about to send email';
            include('scripts/informer.php');
            informer::inform();

            //informer::email('udnweb@gmail.com', 'hi', 'how are you');
        }
    }
    else if ($operation == 'sitemap_index.xml') {
        header('content-type: text/xml');
        echo services::get_sitemap_index($content_type);
    }
    else if ($operation == 'sitemap') {
        header('content-type: text/xml');
        $sitemap = str_replace('sitemap_', '', $sitemap);
        $sitemap = str_replace('.xml', '', $sitemap);
        echo services::get_sitemap($sitemap, $page);
    }
	else if ($operation == 'si') { // sitemap index
        header('content-type: text/xml');
        echo services::get_sitemap_index($content_type);
    }
	else if ($operation == 's') { // sitemap
		//http://udnfind.com/s/movies/0/sitemap.xml
        header('content-type: text/xml');
        echo services::get_sitemap($content_type, $page);
    }
    else if ($operation == 'crawl') {
        //testing crawler on app store...
        services::generate_sitemap("https://itunes.apple.com/en/genre/ios/id36?mt=8", //start at this url
            2, //levels to go deep
            "itunes.apple.com/us/genre/", //prefix: valid urls that will contain useful links of apps
            "itunes.apple.com/us/app/"); //prefix: valid links (we want apps only here)
    }
    else if ($operation == 'dl') //homepage
    {
        $dl_url = $segments[1];
        echo dl::bring(base64_decode($dl_url), $cache_folder);
    }
    else if ($operation == 'd') {
        $provider = $segments[1];
        $search = $segments[2];
        $keywords = $segments[3];
        $bad_keywords = $segments[4];
        echo download::downloadp($provider, $search, $keywords, true, false, $bad_keywords);
    }
    else if ($operation == 'irequest') { //insert request
        include('scripts/informer.php');
        informer::insert_request($_POST);
        echo 'Your request submitted successfully';
    }
    else if ($operation == null) //homepage
    {
		header( 'Location: b' ) ;
    }
    else if ($operation == "cnt")
    {
        include('forms/cnt.php');
    }
/*
    else if ($operation == 'filestube') {
        $phrase = $segments[1];
        $page = $segments[2];
        $xml_string =  util::read_url("http://api.filestube.com/?key=b62a233c73e45d95af4cc193566aa4fc&phrase=$phrase&page=$page");
        $xml_string = str_replace('<?xml version="1.0" encoding="UTF-8"?>', '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsls" href="http://localhost/udnfind/temp/filestube.xsls"?>', $xml_string);
        header('Content-Type: text/xml');
        echo $xml_string;
    }
*/
    else {
        services::page_not_found();
    }

    /*
    if($operation !== 'admin')
	    services::insert_log($udnurl, $operation, $content_type);
    */
}
?>


